﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlServerAdmin;
using IQ.BaseWindows;
using IQ.DbBase;

namespace IQ.DbA
{
    public partial class FrmSyncIndexes : Form
    {
		#region # Constructor # 
        public FrmSyncIndexes()
        {
            InitializeComponent();
            grdServer.AutoGenerateColumns = false;
            grdFields.AutoGenerateColumns = false;
            grdChanges.AutoGenerateColumns = false;
            btnCreate.Enabled = false;
            btnDelIndex.Enabled = false;
        }
		#endregion
		
		#region - Private variables - 
        private SqlMdIndex sqlMD = new SqlMdIndex();
        private bool checkExcludes = false;
        #endregion 

		#region - Private methods - 
        private void setMaxProgress(int value)
        {
            pgbAll.Maximum = value;
        }
        private void setProgress(int value)
        {
            pgbAll.Value = value;
        }
        private void compareOne(string tableNameIndex)
        {
            checkExcludeList();
            sqlMD.CompareIndex(tableNameIndex);
            grdFields.DataSource = sqlMD.FieldDiffs;
        }
        private void checkExcludeList()
        {
            if (checkExcludes)
            {
                sqlMD.SaveExludeList(txtExclude.Lines);
                checkExcludes = false;
            }
        }
        #endregion 
		
		//#region - Public properties / variables - 
		//#endregion

		//#region - Public methods - 
		//#endregion
		
		//#region - Bind Methods - 
		//#endregion
		
		#region - Form Events -
		// This region is for event handler methods that are called without the user knowing about it.
		// Such as the page loading.
        private void FrmSyncTables_Shown(object sender, EventArgs e)
        {
            if (SqlMdFileData.CheckCompareFileExists())
            {
                sqlMD.SetMaxPosition += setMaxProgress;
                sqlMD.SetCurrentPosition += setProgress;
                txtExclude.Lines = sqlMD.Exclusions;
                checkExcludes = false;
                grdChanges.DataSource = sqlMD.Changes;
                grdServer.DataSource = sqlMD.ServerIndexes;
            }
            else
            {
                General.MesDlg("Sql meta data file does not exists, please export meta data and then return.", 'E');
                this.Close();
            }
        }
        private void frmSyncTables_FormClosed(object sender, FormClosedEventArgs e)
        {
            checkExcludeList();
        }
        private void grdFields_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == cffCompare.Index)
            {
                e.CellStyle.ForeColor = SqlConst.GetCompareColor((byte)e.Value);
                e.Value = SqlConst.GetCompareText((byte)e.Value);
                e.FormattingApplied = true;
            }
        }
        private void grdChanges_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            SqlMetaData.ChangeType change = (SqlMetaData.ChangeType)grdChanges[ccChangeType.Index, e.RowIndex].Value;
            btnCreate.Enabled = (change == SqlMetaData.ChangeType.NotExistOnServer);
            btnDelIndex.Enabled = (change == SqlMetaData.ChangeType.NotExistInFile || change == SqlMetaData.ChangeType.IndexStatus);
        }
        private void txtExclude_TextChanged(object sender, EventArgs e)
        {
            checkExcludes = true;
        }
		#endregion 
		
		#region - Control Methods -
		// This region is for event handler methods that are called knowingly by the user.
		// Such as a button being clicked.
        private void grdServer_DoubleClick(object sender, EventArgs e)
        {
            if (grdServer.CurrentRow != null)
            {
                compareOne(grdServer.CurrentRow.Cells[csName.Index].Value.ToString() + " -> " +
                    grdServer.CurrentRow.Cells[csIndex.Index].Value.ToString());
            }
        }

        private void grdChanges_DoubleClick(object sender, EventArgs e)
        {
            if (grdChanges.CurrentRow != null)
            {
                compareOne(grdChanges.CurrentRow.Cells[ccName.Index].Value.ToString());
            }
        }

        private void btnCompare_Click(object sender, EventArgs e)
        {
            if (grdChanges.CurrentRow != null)
            {
                compareOne(grdChanges.CurrentRow.Cells[ccName.Index].Value.ToString());
            }
            else
                General.MesDlg("No table selected to compare.", 'E');
        }
        private void btnCompareAll_Click(object sender, EventArgs e)
        {
            checkExcludeList();
            sqlMD.CompareAllTableIndexes();
        }
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (grdChanges.CurrentRow != null)
            {
                if ((SqlMetaData.ChangeType)grdChanges.CurrentRow.Cells[ccChangeType.Index].Value == SqlMetaData.ChangeType.NotExistOnServer)
                {
                    if (sqlMD.CreateIndex(grdChanges.CurrentRow.Cells[ccName.Index].Value.ToString()))
                        grdChanges.Rows.Remove(grdChanges.CurrentRow);
                }
                else
                    General.MesDlg("Index already on server.", 'I');
            }
        }
        private void btnDelIndex_Click(object sender, EventArgs e)
        {
            if (grdChanges.CurrentRow != null)
            {
                if ((SqlMetaData.ChangeType)grdChanges.CurrentRow.Cells[ccChangeType.Index].Value == SqlMetaData.ChangeType.NotExistInFile)
                {
                    if (General.MesDlg("Delete index: " + grdChanges.CurrentRow.Cells[ccName.Index].Value.ToString() + "?", 'C')
                        == DialogResult.Yes)
                        if (sqlMD.DeleteIndex(grdChanges.CurrentRow.Cells[ccName.Index].Value.ToString()))
                            grdChanges.Rows.Remove(grdChanges.CurrentRow);
                }
            }
            else
                General.MesDlg("Please select an index from the changes grid.", 'I');
        }
        #endregion
    }
}
